package com.mmall.controller.protal;


import com.mmall.common.Const;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.Shipping;
import com.mmall.pojo.User;
import com.mmall.service.IShippingService;
import com.mmall.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/shipping/")
public class ShippingController {

    @Autowired
    private IUserService iUserService;

    @Autowired
    private IShippingService iShippingService;

    @RequestMapping(value = "add.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse add(HttpSession session, Shipping shipping) {
        ServerResponse serverResponse = iUserService.checkAdminRole(session);
        if (!serverResponse.is_success()) {
            return serverResponse;
        }
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        return iShippingService.add(user.getId(), shipping);
    }

    @RequestMapping(value = "delete.do", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse delete(HttpSession session, Integer shippingId) {
        ServerResponse serverResponse = iUserService.checkAdminRole(session);
        if (!serverResponse.is_success()) {
            return serverResponse;
        }
        User user = (User) session.getAttribute(Const.CURRENT_USER);

        return iShippingService.delete(user.getId(), shippingId);
    }


    @RequestMapping(value = "update.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse update(HttpSession session, Shipping shipping) {
        ServerResponse serverResponse = iUserService.checkAdminRole(session);
        if (!serverResponse.is_success()) {
            return serverResponse;
        }
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        return iShippingService.update(user.getId(), shipping);
    }

    @RequestMapping(value = "list.do", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse list(HttpSession session,
                               @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, Integer pageSize) {
        ServerResponse serverResponse = iUserService.checkAdminRole(session);
        if (!serverResponse.is_success()) {
            return serverResponse;
        }
        User user = (User) session.getAttribute(Const.CURRENT_USER);

        return iShippingService.list(user.getId(), pageNum, pageSize);
    }


    @RequestMapping(value = "select.do", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse select(HttpSession session, Integer shippingId) {
        ServerResponse serverResponse = iUserService.checkAdminRole(session);
        if (!serverResponse.is_success()) {
            return serverResponse;
        }
        User user = (User) session.getAttribute(Const.CURRENT_USER);

        return iShippingService.select(user.getId(), shippingId);
    }

}
